package com.dingup.paike.controller.classInfo;

import com.dingup.api.Util.ClassRoomUtil;
import com.dingup.api.adapter.classInfo.ClassInfoAdapter;
import com.dingup.api.base.HttpResContent;
import com.dingup.api.bean.page.PageData;
import com.dingup.api.bean.query.classInfo.ClassInfoForPkBean;
import com.dingup.api.bean.response.SelectBean;
import com.dingup.api.bean.response.classInfo.ClassInfoResponseSelectBean;
import com.dingup.api.model.classInfo.ClassInfo;
import com.dingup.paike.feign.client.classInfo.ClassInfoFeignClient;
import com.dingup.paike.feign.client.user.UserFeignClient;
import com.dingup.paike.service.pk.ClassInfoService;
import com.dingup.paike.service.pk.PkLessonInfoService;
import com.suyeer.basic.util.BHttpResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/9/26.
 */
@Controller
@RequestMapping("/class")
public class ClassController {

    @Autowired
    ClassInfoFeignClient classInfoFeignClient;
    @Autowired
    UserFeignClient userFeignClient;
    @Autowired
    PkLessonInfoService pkLessonInfoService;
    @Autowired
    ClassInfoService classInfoService;

    @RequestMapping(value = "/get_class_info_select_bean.do", method = RequestMethod.POST)
    public void getClassInfoSelectBean(HttpServletRequest request,
                                       HttpServletResponse response) {
        HttpResContent<List<ClassInfoResponseSelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取班级信息");
        try {
            List<ClassInfoResponseSelectBean> res = classInfoFeignClient.getClassInfoSelectBean();
            res.forEach(
                    s -> {
                        if (s.getCampus() != null) {
                            switch (s.getCampus()) {
                                case SHANGHAI:
                                    s.setCampusRooms(ClassRoomUtil.getShangHaiRooms());
                                    break;
                                case NANJING:
                                    s.setCampusRooms(ClassRoomUtil.getNanJingRooms());
                                    break;
                                case YUEDUZU:
                                    s.setCampusRooms(ClassRoomUtil.getBeiJingRooms());
                                    break;
                                case SHUXUEZU:
                                    s.setCampusRooms(ClassRoomUtil.getBeiJingRooms());
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
            );
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_all_observer.do", method = RequestMethod.POST)
    public void getAllObserver(HttpServletRequest request,
                               HttpServletResponse response) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取所有的旁听人");
        try {
            List<SelectBean> res = userFeignClient.getAllUserName();
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_class_info_by_id.do", method = RequestMethod.POST)
    public void getClassInfoById(HttpServletRequest request,
                                 HttpServletResponse response,
                                 Integer classId) {
        HttpResContent<ClassInfoAdapter> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("通过id获取班级信息");
        try {
            ClassInfoAdapter res = classInfoFeignClient.getClassInfoById(classId);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_class_info_for_pk_screen.do", method = RequestMethod.POST)
    public void getClassInfoForPkScreen(HttpServletRequest request,
                                        HttpServletResponse response,
                                        ClassInfoForPkBean bean) {
        HttpResContent<PageData<ClassInfoAdapter>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("筛选班级");
        try {
            List<ClassInfo> classInfos = classInfoFeignClient.getClassInfoForPkScreen(bean);

            List<Map<String, Integer>> counts = pkLessonInfoService.getLessonCount(bean.getIfPreLesson());
            Map<Integer, Integer> countMap = new HashMap<>();
            counts.forEach(
                    c -> {
                        countMap.put(c.get("classId"), c.get("count"));
                    }
            );
            //过滤
            List<ClassInfo> filter = classInfos;
            //如果预排课为null则不过滤
            if (bean.getIfPreLesson() != null){
                filter = classInfos.stream().filter(c -> {
                    if (countMap.keySet().contains(c.getId())){
                        return true;
                    }else{
                        return false;
                    }
                }).collect(Collectors.toList());
            }
            //全部课节数
            List<Map<String, Integer>> countsRes = pkLessonInfoService.getLessonCount(null);
            Map<Integer, String> allCountsResMap = new HashMap<>();
            countsRes.forEach(
                    c -> {
                        allCountsResMap.put(c.get("classId"), String.valueOf(c.get("count")));
                    }
            );
            //预排课数
            List<Map<String, Integer>> preLessonCountsRes = pkLessonInfoService.getLessonCount(true);
            Map<Integer, Integer> preLessonCountsResMap = new HashMap<>();
            preLessonCountsRes.forEach(
                    c -> {
                        preLessonCountsResMap.put(c.get("classId"), c.get("count"));
                    }
            );
            //分页
            List<ClassInfoAdapter> res = filter.stream().skip(bean.getBefore()).limit(bean.getRows())
                    .map(ClassInfoAdapter::new).collect(Collectors.toList());
            res.forEach(
                    c -> {
                        if (allCountsResMap.get(c.getId()) != null) {
                            c.setPkLessonNum(Integer.valueOf(allCountsResMap.get(c.getId())));
                        } else {
                            c.setPkLessonNum(0);
                        }
                        //返回前端是否有预排课表示
                        if (preLessonCountsResMap.get(c.getId()) != null) {
                            c.setIfHasPreLesson(true);
                        } else {
                            c.setIfHasPreLesson(false);
                        }
                    }
            );
            PageData<ClassInfoAdapter> pageData = new PageData<>();
            pageData.setData(res);
            pageData.setRows(bean.getRows());
            pageData.setPage(bean.getPage());
            pageData.setTotal(filter.size());
            saveSuccessMsgAndData(hrc, pageData);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_class_teacher.do", method = RequestMethod.POST)
    public void getClassTeacher(HttpServletRequest request,
                                HttpServletResponse response,
                                Integer classId) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取班级的老师");
        try {
            List<SelectBean> classTeacher = classInfoService.getClassTeacher(classId);
            saveSuccessMsgAndData(hrc, classTeacher);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
}
